define(function (require , exports , module) {
    require('dmuploader');
    require('sortable');

    var $       = require('jquery');
    var common  = require('common/common');
    var layer   = require('layer');

    var ajaxAddBanner = function () {
        $('.form-submit').bind('click' , function () {
            var layerLoad = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            var formData = {};
            var formDataItem = $('.form-control');

            formDataItem.each(function (index , item) {
                var __item = $(item);
                if(__item.attr('name') != undefined) {
                    if(__item.attr('type') == 'radio'){
                        if(__item.prop('checked')){
                            formData[__item.attr('name')] = __item.val();
                        }
                    }else {
                        formData[__item.attr('name')] = __item.val();
                    }
                }
            });

            var goodsImgs = $('#demo-files').find('.goods-imgs');

            if(goodsImgs.length == 0){
                layer.alert('请先上传商品图片');
            }else {
                var imgs = [];
                var sort = 1;
                goodsImgs.each(function (index , img) {
                    var img_tmp = {};
                    img_tmp['imgUrl']   = $(img).attr('src');
                    img_tmp['imgId']    = $(img).attr('img-id');
                    img_tmp['sort']     = sort;
                    imgs.push(img_tmp);
                    sort++;
                });

                formData['img'] = imgs[0]['imgUrl'];

                console.log(formData);

                common.ajaxPost('/admin/banner/add', formData, function (error, data) {
                    if (error == null && data.code == 0) {
                        layer.msg('添加成功');
                        window.location.reload();
                    } else if (error == null && data.code != 0) {
                        layer.alert(data.msg);
                    } else {
                        layer.alert(error.statusText);
                    }
                    layer.close(layerLoad);
                });
            }
        });
    };

    exports.ajaxAddBanner = ajaxAddBanner;
});